﻿#region << 文 件 说 明 >>

/*----------------------------------------------------------------
// 文件名称：MainWindow
// 创 建 者：杨程
// 创建时间：2021/11/3 17:20:09
// 文件版本：V1.0.0
// ===============================================================
// 功能描述：
//
//
//----------------------------------------------------------------*/

#endregion


namespace Vampirewal.Core.WPF.Theme;

/// <summary>
/// 适用于主窗体的页面
/// </summary>
public class MainWindowBase : WindowBase
{
    private ColumnDefinition leftcontent { get; set; }

    /// <summary>
    /// 记录最右位置
    /// </summary>
    private GridLength _lastLength;

    public UIElement LeftContent { get; set; }

    public List<UIElement> MainAreas { get; set; } = new List<UIElement>();

    /// <summary>
    /// 拖动条样式
    /// </summary>
    public ControlTemplate GridSplitterStyle { get; set; }

    static MainWindowBase()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MainWindowBase), new FrameworkPropertyMetadata(typeof(MainWindowBase)));
    }

    /// <summary>
    /// 构造函数
    /// </summary>
    public MainWindowBase()
    {
        var BaseStyle = res["MainWindow"] as Style;

        this.Style = BaseStyle;
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        //var aaa = base.Resources["WindowBase"] as Style;//如果前端添加引用之后，可以使用这个方法

        #region 顶部模块

        var MainArea = this.Template.FindName("MainAreaMenu", this) as StackPanel;

        if (MainAreas.Count > 0)
        {
            foreach (var item in MainAreas)
            {
                MainArea.Children.Add(item);
            }
        }
        else
        {
            MainAreaHeight = 0;
        }

        #endregion

        #region 拖动条设置

        var GS = this.Template.FindName("gs", this) as GridSplitter;

        GS.MouseDoubleClick += GS_MouseDoubleClick;

        _lastLength = new GridLength(LeftMenuMaxWidth, GridUnitType.Star);

        #region 自定义GridSplitter模版

        if (GridSplitterStyle != null)
        {
            if (GridSplitterStyle.TargetType == typeof(GridSplitter))
            {
                GS.Template = GridSplitterStyle;
            }
        }

        #endregion

        #endregion

        #region 左侧菜单栏

        leftcontent = this.Template.FindName("leftcontent", this) as ColumnDefinition;

        if (LeftContent == null)
        {
            leftcontent.Width = new GridLength(0, GridUnitType.Star);
            GS.Visibility = Visibility.Collapsed;
        }

        #endregion
    }

    private void GS_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        if (leftcontent != null)
        {
            if (leftcontent.Width.Value > 0)
            {
                leftcontent.Width = new GridLength(0, GridUnitType.Star);
            }
            else
            {
                leftcontent.Width = _lastLength;
            }
        }
    }

    ///// <summary>
    ///// 关闭窗体（可重写，需保留base.CloseWin_Click）
    ///// </summary>
    ///// <param name="sender"></param>
    ///// <param name="e"></param>
    //protected virtual void CloseButton_Click(object sender, RoutedEventArgs e)
    //{
    //    WindowsManager.CloseWindow(this);
    //    //this.Close();
    //}

    protected override void OnStyleChanged(Style oldStyle, Style newStyle)
    {
        base.OnStyleChanged(oldStyle, newStyle);
    }

    #region 依赖属性

    #region 左侧菜单栏最大宽度

    public int LeftMenuMaxWidth
    {
        get { return (int)GetValue(LeftMenuMaxWidthProperty); }
        set { SetValue(LeftMenuMaxWidthProperty, value); }
    }

    // Using a DependencyProperty as the backing store for LeftMenuMaxWidth.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty LeftMenuMaxWidthProperty =
        DependencyProperty.Register("LeftMenuMaxWidth", typeof(int), typeof(MainWindowBase), new PropertyMetadata(230));

    #endregion

    #region 顶部模块区域高度

    public int MainAreaHeight
    {
        get { return (int)GetValue(MainAreaHeightProperty); }
        set { SetValue(MainAreaHeightProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MainAreaHeight.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MainAreaHeightProperty =
        DependencyProperty.Register("MainAreaHeight", typeof(int), typeof(MainWindowBase), new PropertyMetadata(80));

    #endregion

    #region 开启托盘图标

    /// <summary>
    /// 是否开启托盘图标
    /// </summary>
    protected bool UseVampirewalNotifyIcon { get; set; }

    protected override void Window_Loaded(object sender, RoutedEventArgs e)
    {
        base.Window_Loaded(sender, e);

        if (UseVampirewalNotifyIcon)
        {
            var menuItems = SetVampirewalNotifyIconContextMenu();

            VampirewalNotifyIcon notify = new VampirewalNotifyIcon(this, menuItems,this.Title);

        }
    }

    /// <summary>
    /// 设置托盘图标的菜单
    /// </summary>
    /// <returns></returns>
    protected virtual List<MenuItem> SetVampirewalNotifyIconContextMenu()
    {
        return new List<MenuItem>()
        {
            new MenuItem()
            {
                Header="退出",
                Command=CloseWindowCommand,
                
            }
        };
    }

    #endregion

    #region 使用等待覆盖



    public bool IsLoading
    {
        get { return (bool)GetValue(IsLoadingProperty); }
        set { SetValue(IsLoadingProperty, value); }
    }

    // Using a DependencyProperty as the backing store for IsLoading.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty IsLoadingProperty =
        DependencyProperty.Register("IsLoading", typeof(bool), typeof(MainWindowBase), new PropertyMetadata(false));




    public string LoadingTips
    {
        get { return (string)GetValue(LoadingTipsProperty); }
        set { SetValue(LoadingTipsProperty, value); }
    }

    // Using a DependencyProperty as the backing store for LoadingTips.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty LoadingTipsProperty =
        DependencyProperty.Register("LoadingTips", typeof(string), typeof(MainWindowBase), new PropertyMetadata("加载中，请稍等……"));


    #endregion

    #endregion
}